package com.shuoyue.ycgk.ui.shop.address;


import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.address.OrderAddressBean;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface SelectAddressContract {
    class Model extends BaseModel {
        public Observable<BaseResult<List<OrderAddressBean>>> getAddress() {
            return RetrofitClient.getInstance().getApi().queryAddressList();
        }

        public Observable<BaseResult<String>> delete(int id) {
            return RetrofitClient.getInstance().getApi().deleteAddresss(id);
        }

        public Observable<BaseResult<String>> edit(Integer id,
                                                   String provinceName,
                                                   String cityName,
                                                   String regionName,
                                                   String address,
                                                   String contact,
                                                   String phone,
                                                   int isDefault,
                                                   int provinceId,
                                                   int cityId,
                                                   int regionId) {
            return RetrofitClient.getInstance().getApi().editAddress(id, provinceName, cityName, regionName,
                    address, contact, phone, isDefault, provinceId, cityId, regionId);
        }
    }

    interface View extends BaseView {
        void setAddress(List<OrderAddressBean> data);

        void editSuc();

        void addSuc();

        void deleteSuc();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getAddress() {
            apply(model.getAddress())
                    .subscribe(new ApiSubscriber<Optional<List<OrderAddressBean>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<OrderAddressBean>> listOptional) {
                            super.onNext(listOptional);
                            mView.setAddress(listOptional.getIncludeNull());
                        }
                    });
        }

        public void delete(int id) {
            apply(model.delete(id))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> stringOptional) {
                            super.onNext(stringOptional);
                            mView.deleteSuc();
                        }
                    });
        }

        public void edit(OrderAddressBean addressBean) {

            apply(model.edit(addressBean.getId(),
                    addressBean.getProvinceName(),
                    addressBean.getCityName(),
                    addressBean.getRegionName(),
                    addressBean.getAddress(),
                    addressBean.getContact(),
                    addressBean.getPhone(),
                    addressBean.getIsDefault(),
                    addressBean.getProvinceId(),
                    addressBean.getCityId(),
                    addressBean.getRegionId()))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> stringOptional) {
                            super.onNext(stringOptional);
                            mView.editSuc();
                        }
                    });
        }
    }

}
